package com.dalongxin.work.page.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dalongxin.work.BaseApplication;
import com.dalongxin.work.BuildConfig;
import com.dalongxin.work.R;
import com.dalongxin.work.page.AboutUsActivity;
import com.dalongxin.work.page.LoginActivity;
import com.dalongxin.work.utils.StatusBarUtil;
import com.dalongxin.work.utils.ToastUtil;
import com.dalongxin.work.view.MyCommonDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.fragment_main_mine)
public class MineFragment extends BaseFragment implements View.OnClickListener {

    @ViewInject(R.id.tool_bar)
    private Toolbar toolbar;
    @ViewInject(R.id.title_text)
    private TextView titleText;
    @ViewInject(R.id.user_name)
    private TextView userName;
    @ViewInject(R.id.layout_about_us)
    private RelativeLayout layoutAboutus;
    @ViewInject(R.id.text_about_us)
    private TextView textAboutus;
    @ViewInject(R.id.layout_version)
    private RelativeLayout layoutVersion;
    @ViewInject(R.id.text_version)
    private TextView textVersion;
    @ViewInject(R.id.layout_logout)
    private RelativeLayout layoutLogout;

    private MyCommonDialog commonDialog;

    private View rootView;
    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null) {
            rootView = x.view().inject(this, inflater, container);
        }
        titleText.setText("我的");
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);

        userName.setText(getDefaultAccount().getUserName());
        textVersion.setText("V" + BuildConfig.VERSION_NAME + " build" + BuildConfig.VERSION_CODE);
        layoutAboutus.setOnClickListener(this);
        layoutVersion.setOnClickListener(this);
        layoutLogout.setOnClickListener(this);
        return rootView;
    }

    private void initDialog() {
        commonDialog = new MyCommonDialog(getActivity(), "提示消息", "确定要退出吗？", "取消", "确认");
        commonDialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (commonDialog.isShowing())
                    commonDialog.dismiss();
                BaseApplication.getInstance().clearDefaultAccount();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        commonDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (commonDialog.isShowing()) {
                    commonDialog.dismiss();
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_about_us:
                Intent intent = new Intent(getActivity(), AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_version:
                //ToastUtil.showShort("已经是最新版本");
                break;
            case R.id.layout_logout:
                if(commonDialog == null) {
                    initDialog();
                }
                if(!commonDialog.isShowing()) {
                    commonDialog.show();
                }
                break;
        }
    }
}
